-- 应用程序快捷键
-- https://www.jianshu.com/p/937bf92ac39c

local key2App = {
    f = 'Finder',
    g = 'Google Chrome',
    t = 'iTerm',
    -- v = 'Visual Studio Code',
    c = 'iCoding',
    b = '如流',
    w = '微信',
    h = 'Firefox',
    s = 'Navicat for MySQL',
    -- s = 'Sequel Pro',
    d = 'Postman',
    m = 'Microsoft Outlook',
    e = 'GoLand',
    p = 'Typora',
}

for key, app in pairs(key2App) do
    hs.hotkey.bind({"alt"}, key, function()
        open(app)
        -- toggle_application(app)
    end) 
end

-- version 1
function open(name)
    hs.application.launchOrFocus(name)
    if name == 'Finder' or name == '微信'then
        hs.appfinder.appFromName(name):activate()
    end
end

-- version 2
-- local hotkey = require 'hs.hotkey'
-- local window = require 'hs.window'
-- local application = require 'hs.application'

-- -- Toggle an application between being the frontmost app, and being hidden
-- function toggle_application(_app)
--     -- finds a running applications
--     local app = application.find(_app)
--     -- @TODO 微信最小化以后无法拉起
--     if _app == 'Finder' or _app == '微信' then
--           hs.application.launchOrFocus(_app)
--           if _app == 'Finder' or _app == '微信' then
--               hs.appfinder.appFromName(_app):activate()
--           end
--           return
--     end

--     if not app then
--         -- application not running, launch app
--         application.launchOrFocus(_app)
--         return
--     end

--     -- application running, toggle hide/unhide
--     local mainwin = app:mainWindow()
--     if mainwin then
--         if true == app:isFrontmost() then
--             mainwin:application():hide()
--         else
--             mainwin:application():activate(true)
--             mainwin:application():unhide()
--             mainwin:focus()
--         end
--     else
--         -- no windows, maybe hide
--         if true == app:hide() then
--             -- focus app
--             application.launchOrFocus(_app)
--         else
--             -- nothing to do
--         end
--     end
-- end
